package agent

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/base-common/headInfo"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/agentClient/rpc"
	user "gitee.com/liuxuezhan/ar-platform/rpc-client/loginClient/client"
	"github.com/spf13/cast"
	"github.com/zeromicro/go-zero/core/logc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type RebatesSettingSwitchLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 返佣配置开关
func NewRebatesSettingSwitchLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RebatesSettingSwitchLogic {
	return &RebatesSettingSwitchLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *RebatesSettingSwitchLogic) RebatesSettingSwitch(req *types.RebatesSettingSwitchRequest) (resp *types.Response, err error) {
	token, err := l.svcCtx.LoginRpc.ParseToken(l.ctx, &user.ParseTokenRequest{
		Token: headInfo.GetJwtToken(l.ctx),
	})
	if err != nil {
		logc.Error(l.ctx, err)
		return nil, err
	}
	adminName := token.UserName
	_, err = l.svcCtx.AgentRPC.RebatesSettingSwitch(l.ctx, &rpc.AgentRebatesSettingSwitchRequest{
		AgentId: req.AgentId,
		Status:  cast.ToInt32(req.Status),
		Admin:   adminName,
	})
	if err != nil {
		logc.Error(l.ctx, err)
		return nil, err
	}
	resp = &types.Response{}
	return
}
